from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from . import service
# Create your views here.

def index(request):
    ts = service.getAllTitles()
    return render(request, "poem/list.html",{"titles":ts})

def detail(request, name):
    poem = service.getPoemByTitle(name)
    return render(request, 'poem/shige.html', {"shi":poem})

def create(request):
    return render(request, 'poem/create.html',{})

def svc_create(request):
    # 把用户传来的title content给写入数据库，并且跳转到index诗歌列表页面
    # 1 获取数据
    t = request.POST['title']
    c = request.POST['content']
    # 2 写数据库
    service.addPoem(t,c)
    # 3 跳转
    return HttpResponseRedirect(reverse("poem:index"))